package com.fengba.nanzhi.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.fengba.nanzhi.fengba.R;

import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by duan on 2018/4/10.
 */

public class Function {

    /**
     * 获取status宽度
     * @param context
     * @return 像素
     */
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;         // 屏幕宽度（像素）
        return width;
    }

    /**
     * 获取屏幕高度
     * @param context
     * @return 像素
     */
    public static int getScreenHeight(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int height = dm.heightPixels;       // 屏幕高度（像素）
        return height;
    }

    /**
     * 获取status宽度
     * @param context
     * @return dp
     */
    public static int getScreenWidthReturnDP(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        float density = dm.density;
        int width = dm.widthPixels;         // 屏幕宽度（像素）
        int screenWidth = (int) (width / density);  // 屏幕宽度(dp)
        return screenWidth;
    }

    /**
     * 获取屏幕高度
     * @param context
     * @return dp
     */
    public static int getScreenHeightReturnDP(Context context) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        float density = dm.density;
        int height = dm.heightPixels;       // 屏幕高度（像素）
        int screenHeight = (int) (height / density);// 屏幕高度(dp)
        return screenHeight;
    }

    /**
     * 获取status高度
     * @param context
     * @return
     */
    public static int getStatusBarHeight(Context context) {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        return resources.getDimensionPixelSize(resourceId);
    }

    /**
     * 根据手机分辨率从DP转成PX
     * @param context
     * @param dpValue
     * @return
     */
    public static int dip2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率PX(像素)转成DP
     * @param context
     * @param pxValue
     * @return
     */
    public static int px2dip(Context context, float pxValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 获取图片名称获取图片的资源id的方法
     * @param name
     * @return
     */
    public static int getResourceId(String name) {
        try {
            // 根据图片资源的文件名获得Field对象
            Field field = R.mipmap.class.getField(name);
            // 取得并返回资源ID
            return Integer.parseInt(field.get(null).toString());
        } catch (Exception e) {}
        return 0;
    }

    /**
     * 掉此方法输入所要转换的时间输入例如（"2014年06月14日16时09分00秒"）返回时间戳
     *
     * @param time
     * @return
     */
    public static long string2Time(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                Locale.CHINA);
        Date date;
        long times = 0;
        try {
            date = sdr.parse(time);
            times = date.getTime();
            //String stf = String.valueOf(l);
            //times = stf.substring(0, 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return times;
    }

    /**
     * 隐藏手机号码中间四位****
     * @param mobile
     * @return
     */
    public static String phoneHide4Start(String mobile){
        String maskNumber = mobile.substring(0,3)+"****"+mobile.substring(7,mobile.length());
        return maskNumber;
    }

    /**
     * double转String,保留小数点后两位
     * @param num
     * @return
     */
    public static String doubleToString(double num){
        //使用0.00不足位补0，#.##仅保留有效位
        return new DecimalFormat("0.00").format(num);
    }

    public static String userNameHide(String name){
        String maskNumber="";
        String name1 = name.substring(0,1);
        for (int i=0;i<=name.length()-2;i++){
            maskNumber += "*";
        }

        return name1 + maskNumber;
    }

    public static String TimeHide(String Time){
        String maskNumber = Time.substring(0,10);
        return maskNumber;
    }


}
